package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /***
     * 新增菜品/套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }

    /**
     * 菜品/套餐分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page,Integer pageSize){
        //1准备分页条件
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //2准备查询条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
        //3.执行方法
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id来删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id){
        //categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功");
    }


    /**
     * 根据ID修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据菜品来分类查询
     * @param
     * @return
     */
     @GetMapping("/list")
     public R<List<Category>> list(Integer type){
         //准备查询条件
         QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq(type != null,"type",type);
         //调用方法获取菜品的集合
         List<Category> list = categoryService.list(queryWrapper);
         return R.success(list);

     }
}
